package com.zjxny.jobs;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.erupt.core.annotation.EruptHandlerNaming;
import xyz.erupt.job.handler.EruptJobHandler;

@Slf4j
@Service
@EruptHandlerNaming("掩码VIN前11位")
public class FixVihMaskJob implements EruptJobHandler {
	
	@Resource
	JdbcTemplate jdbcTemplate;
	
	@Override
	public String exec(String code, String param) {
		String sql = "select id,clsbdh from car_info where clsbdh is not null";
		List<Map<String,Object>> lists = jdbcTemplate.queryForList(sql);
		
		for(Map<String,Object> map: lists) {
			String vin =(String) map.get("clsbdh");
			Long id = (Long) map.get("id");
			
			String prefix = StrUtil.sub(vin, 0, vin.length() - 6);
	        String maskedChars = StrUtil.repeat('*', prefix.length());
	        String vinMask = maskedChars + StrUtil.sub(vin,prefix.length(),vin.length());	
	        
	        String uSql = "update car_info set clsbdh_mask='"+vinMask+"' where id="+id;
	        int affectNum = jdbcTemplate.update(uSql);
	        
	        log.info("carId={},clsbdh={},affectNum={}",id,vin,affectNum);
		}
		
		return null;
	}
}
